package com.ssm.zyn.action;

import com.ssm.zyn.eo.RoleEOpage;
import com.ssm.zyn.model.Role;
import com.ssm.zyn.service.Roleservice;
import com.ssm.zyn.tool.ResponseMessage;
import com.ssm.zyn.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags="role")
@RestController
@CrossOrigin
public class RoleController {
    @Autowired
    private Roleservice roleservice;
    /*@ApiOperation(value = "角色添加方法")
    @RequestMapping(value="/addRole",method = RequestMethod.GET)
    public int AddRole(Role role){
        int IsAdd=0;
        //传入数据的判断
        //（1）实体对象不能为空
        if(role!=null){
            //(2)角色名称不能为空
            if(role.getRname()!=null){
                //(3)添加角色数据
                IsAdd=roleservice.AddtRole(role);
            }
        }
        return IsAdd;
    }*/
    @ApiOperation(value = "角色添加方法")
    @RequestMapping(value="/add",method = RequestMethod.GET)
    public ResponseMessage addRole(Role role){
        int IsAdd=0;
        //传入数据的判断
        //        //（1）实体对象不能为空
        if(role!=null){
                IsAdd=roleservice.AddtRole(role);
                if(IsAdd==1){
                    return Result.success("保存成功");
                }else {
                    return Result.error("保存失败");
                }
        }
        return Result.error("保存失败");
    }
    @ApiOperation(value = "角色删除方法")
    @RequestMapping(value="/delRole",method = RequestMethod.GET)
    public ResponseMessage deRole(int rid){
        if (rid!=0){
            int delInt=0;
           delInt=roleservice.DeleteRole(rid);
           if(delInt>0){
               return Result.success("删除成功");
           }else {
               return Result.error("与中间表有关联，删除失败");
           }
        }else {
            return Result.error("删除的主键不能为0,请重新输入");
        }
    }
    @ApiOperation(value = "角色修改方法")
    @RequestMapping(value="/updateRole",method = RequestMethod.GET)
    public ResponseMessage updateRole(Role role){
        int isUpdate=0;
        //传入的参数不能为空
        if(role!=null){
            //参数当中主键不能为0

            if (role.getRid()!=0){
                isUpdate=roleservice.UpdateRole(role);
                if(isUpdate==1){

                    return Result.success("更新成功");
                }else {
                    return Result.error("更新失败");
                }
            }
        }
        return  Result.error("传入参数不正确");
    }
    @ApiOperation(value = "获取全部角色信息")
    @RequestMapping(value = "/getAll",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public ResponseMessage getAll(@RequestBody RoleEOpage roleEOpage){
        Map<String, List> map=new HashMap<>();
        List<RoleEOpage> listRole=roleservice.getWhereAll(roleEOpage);
        map.put("list",listRole);
        return Result.success("200",map);
    }
    @ApiOperation(value = "根据用户名获取角色信息")
    @RequestMapping(value = "/getRoleByRname",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    @CrossOrigin
    public ResponseMessage getRoleByRname(@RequestParam String rname){
        Map<String, List> map=new HashMap<>();
        List<RoleEOpage> listRole=roleservice.getRoleByRname(rname);
        map.put("list",listRole);
        return Result.success("200",map);
    }


}
